'use strict';

module.exports = app => {
    return class UserService extends app.Service {
        constructor(ctx) {
            super(ctx);
        }
        /**
         * 获取用户信息
         */
        async userinfo(params) {
            let url = this.app.urls('userinfo');
            this.app.logger.info(url);
            const result = await this.ctx.fetch(url, {
                method: 'get',
                dataType: 'json',
                data:{
                    id:params.id,
                },
            });
            this.app.logger.info(result.data);
            let data = result.data;

            if (data.code != 200){
                throw new Error(data.message ? data.message : 'error');
            }

            return data.data;
        }

        /*
        获取用户发过的帖子
         */
        async userpost(params){
            let url = this.app.urls('userpost');
            this.app.logger.info(url);
            const result = await this.ctx.fetch(url,{
                method:'get',
                dataType:'json',
                data:{
                    objectId:params.id,
                },
            });
            this.app.logger.info(result.data);
            let data = result.data;

            if (data.code != 200){
                throw new Error(data.message ? data.message : 'error');
            }

            return data.data;
        }
    }
};
